<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" size="small" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="serviceOrderSn">
        <el-input
          v-model="queryParams.serviceOrderSn"
          placeholder="请输入服务订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务名称" prop="serviceName">
        <el-input
          v-model="queryParams.serviceName"
          placeholder="请输入服务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单用户" prop="keywords">
        <el-input
          v-model="queryParams.keywords"
          placeholder="请输入下单用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="dateValue">
        <el-date-picker
          v-model="queryParams.dateValue"
          type="daterange"
          align="right"
          size="small"
          unlink-panels
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="serviceOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="serviceOrderSn">
        <template slot-scope="scope">
          <span style="cursor: pointer;color: #1c84c6;" @click="goDetail(scope.row)" v-hasPermi="['biz:serviceOrder:query']">{{scope.row.serviceOrderSn}}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务名称" align="center" prop="serviceName" />
      <el-table-column label="服务商" align="center" prop="acceptUserName"  />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="订单金额(陪护豆/个)" align="center" prop="amount" />
      <el-table-column label="下单用户" align="center" prop="userId" >
        <template slot-scope="scope">
          <span>{{scope.row.orderUserName}}({{scope.row.orderUserPhone}})</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.biz_service_order_status" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-circle-close"
            @click="handleCancel(scope.row)"
            v-if="scope.row.orderStatus === '1'"
            v-hasPermi="['biz:serviceOrder:cancel']"
          >取消</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改服务订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="服务订单编号" prop="serviceOrderSn">
          <el-input v-model="form.serviceOrderSn" maxlength="50" placeholder="请输入服务订单编号" />
        </el-form-item>
        <el-form-item label="下单用户id" prop="userId">
          <el-input v-model="form.userId" maxlength="20" placeholder="请输入下单用户id" />
        </el-form-item>
        <el-form-item label="预约时间" prop="appointmentDate">
          <el-date-picker clearable size="small"
            v-model="form.appointmentDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择预约时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预约医院id" prop="hospitalId">
          <el-input v-model="form.hospitalId" maxlength="20" placeholder="请输入预约医院id" />
        </el-form-item>
        <el-form-item label="联系人手机号" prop="contactPhone">
          <el-input v-model="form.contactPhone" maxlength="20" placeholder="请输入联系人手机号" />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactName">
          <el-input v-model="form.contactName" maxlength="50" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="特殊需求" prop="specialNeeds">
          <el-input v-model="form.specialNeeds" maxlength="255" placeholder="请输入特殊需求" />
        </el-form-item>
        <el-form-item label="商品id " prop="productId">
          <el-input v-model="form.productId" maxlength="20" placeholder="请输入商品id " />
        </el-form-item>
        <el-form-item label="购买数量" prop="num">
          <el-input v-model="form.num" maxlength="5" placeholder="请输入购买数量" />
        </el-form-item>
        <el-form-item label="订单金额【陪护豆】" prop="amount">
          <el-input v-model="form.amount" maxlength="10,2" placeholder="请输入订单金额【陪护豆】" />
        </el-form-item>
        <el-form-item label="派单时间" prop="dispatchTime">
          <el-date-picker clearable size="small"
            v-model="form.dispatchTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择派单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完成时间" prop="finishTime">
          <el-date-picker clearable size="small"
            v-model="form.finishTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="取消时间" prop="cancelTime">
          <el-date-picker clearable size="small"
            v-model="form.cancelTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择取消时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" maxlength="255" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="submitLoading" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-drawer
      :title="title"
      :visible.sync="detailOpen"
      size="50%"
      :show-close="true"
      append-to-body>
      <el-form ref="form" :model="form" label-width="140px" size="mini" >
        <p style="margin-left: 20px">订单信息</p>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="订单编号：">{{form.serviceOrderSn}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单状态：">
              <dict-tag :options="dict.type.biz_service_order_status" :value="form.orderStatus"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单数量：">{{form.num}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订单金额：">{{form.amount}}陪护豆</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间：">{{form.createTime}}</el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.cancelTime">
            <el-form-item label="取消时间：">{{form.cancelTime}}</el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.finishTime">
            <el-form-item label="完成时间：">{{form.finishTime}}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form ref="form" :model="form" label-width="140px" size="mini" >
        <p style="margin-left: 20px">用户信息</p>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名称：">{{form.orderUserName}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号：">{{form.orderUserPhone}}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form ref="form" :model="form" label-width="140px" size="mini" >
        <p style="margin-left: 20px">商品信息</p>
        <el-divider></el-divider>
        <el-row v-if="form.buyType === '1'">
          <el-col :span="12">
            <el-form-item label="服务名称：">{{form.serviceInfoVo.name}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务类型：">
              <dict-tag :options="dict.type.biz_service_type" :value="form.serviceInfoVo.type"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时长："> {{form.serviceInfoVo.serviceTime}} {{form.serviceInfoVo.serviceTimeUnit}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格："> {{form.serviceInfoVo.price}}陪护豆</el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="form.buyType === '2'">
          <el-col :span="12">
            <el-form-item label="套餐名称：">{{form.comboInfoResultVo.comboName}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐价格：">{{form.comboInfoResultVo.price}}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="包含服务：">
              <div>
                <el-table :data="form.comboInfoResultVo.serviceInfoResultVOList" style="width: 98%" border>
                  <el-table-column label="序号" align="center" type="index"/>
                  <el-table-column label="服务名称" align="center" prop="name"/>
                  <el-table-column label="服务类型" align="center" prop="type">
                    <template slot-scope="scope">
                      <dict-tag :options="dict.type.biz_service_type" :value="scope.row.type"/>
                    </template>
                  </el-table-column>
                  <el-table-column label="服务时长" align="center" prop="name">
                    <template slot-scope="scope">
                      <span>{{scope.row.serviceTime}}{{scope.row.serviceTimeUnit}}</span>
                    </template>
                  </el-table-column>
                  <el-table-column label="服务价格(陪护豆)" align="center" prop="price"/>
                </el-table>
              </div>

            </el-form-item>

          </el-col>
        </el-row>
      </el-form>
      <el-form ref="form" :model="form" label-width="140px" size="mini"  v-if="form.orderEvaluation">
        <p style="margin-left: 20px">评价信息</p>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="评价时间：">{{form.orderEvaluation.evaluationTime}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评分：">{{form.orderEvaluation.score}}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="评价图片：">
              <el-image  v-for="(img,index) in form.orderEvaluation.imageArr" :key="index"
                         :src="img" style="width: 140px;height: 80px;margin-left: 10px" v-if="form.orderEvaluation.images"
                         :preview-src-list="form.orderEvaluation.imageArr"> </el-image>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="评价内容：">{{form.orderEvaluation.content}}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form ref="form" :model="form" label-width="140px" size="mini"  v-if="form.orderComplaint">
        <p style="margin-left: 20px">投诉信息</p>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="投诉时间：">{{form.orderComplaint.complaintTime}}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="投诉图片：">
              <el-image  v-for="(img,index) in form.orderComplaint.imageArr" :key="index"
                         :src="img" style="width: 140px;height: 80px;margin-left: 10px" v-if="form.orderComplaint.images"
                         :preview-src-list="form.orderComplaint.imageArr"> </el-image>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投诉内容：">{{form.orderComplaint.content}}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-drawer>

  </div>
</template>

<script>
import { listServiceOrder, getServiceOrder, delServiceOrder, addServiceOrder, updateServiceOrder, changeStatus,cancel} from "@/api/biz/serviceOrder";

export default {
  name: "ServiceOrder",
  dicts:['biz_service_order_status','biz_service_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 服务订单表格数据
      serviceOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serviceOrderSn: undefined,
        type: undefined,
        userId: undefined,
        appointmentDate: undefined,
        hospitalId: undefined,
        contactPhone: undefined,
        contactName: undefined,
        specialNeeds: undefined,
        buyType: undefined,
        productId: undefined,
        num: undefined,
        amount: undefined,
        orderStatus: undefined,
        dispatchTime: undefined,
        finishTime: undefined,
        cancelTime: undefined,
        serviceName:undefined,
        keywords:undefined,
        dateValue:undefined,
        startTime:undefined,
        endTime:undefined,
      },
      // 表单参数
      form: {},
      submitLoading: false,
      // 表单校验
      rules: {
        serviceOrderSn: [
          { required: true, message: "服务订单编号不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "订单类型【1=用户订单 2=客服代下单】不能为空", trigger: "change" }
        ],
        buyType: [
          { required: true, message: "购买的类型【1=服务 2=套餐】不能为空", trigger: "change" }
        ],
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一天',
          onClick(picker) {
            const end = new Date();
            picker.$emit('pick', [end, end]);
          }
        },{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询服务订单列表 */
    getList() {
      this.loading = true;
      listServiceOrder(this.queryParams).then(response => {
        this.serviceOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getIndex($index) {
      //表格序号
      return (this.queryParams.pageNum - 1) * this.queryParams.pageSize + $index + 1
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        serviceOrderSn: null,
        type: null,
        userId: null,
        appointmentDate: null,
        hospitalId: null,
        contactPhone: null,
        contactName: null,
        specialNeeds: null,
        buyType: null,
        productId: null,
        num: null,
        amount: null,
        orderStatus: "0",
        dispatchTime: null,
        finishTime: null,
        cancelTime: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
        orderEvaluation:{
        },
        orderComplaint:{

        },
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加服务订单";
    },
    handleChangeStatus(row) {
      let statusText = row.status === 'active' ? '禁用' : '启用'
      let status = row.status === 'active' ? 'inactive' : 'active'
      const ids = [row.id] || this.ids;
      this.$confirm('是否确认' + statusText + '该项数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return changeStatus({ids: ids, status: status});
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(function () {
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getServiceOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改服务订单";
      });
    },

    goDetail(row) {
      this.reset();
      const id = row.id || this.ids
      getServiceOrder(id).then(response => {
        this.form = response.data;
        if(this.form.orderEvaluation.images){
          this.form.orderEvaluation.imageArr = JSON.parse(this.form.orderEvaluation.images);
        }
        if(this.form.orderComplaint.images){
          this.form.orderComplaint.imageArr = JSON.parse(this.form.orderComplaint.images);
        }
        console.log("this.form",this.form);
        this.detailOpen = true;
        this.title = "服务订单详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id != null) {
            updateServiceOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.submitLoading = false;
              this.getList();
            }).catch(() => {
              this.submitLoading = false;
            });
          } else {
            addServiceOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.submitLoading = false;
              this.getList();
            }).catch(() => {
              this.submitLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除服务订单编号为"' + ids + '"的数据项？').then(function() {
        return delServiceOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleCancel(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认取消服务订单编号为"' + row.serviceOrderSn + '"的数据项？').then(function() {
        return cancel(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('biz/serviceOrder/export', {
        ...this.queryParams
      }, `serviceOrder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
